<script src=../priorities/resources/common.js></script>
<script src=../resources/testharness.js></script>
<script src=../resources/testharnessreport.js></script>

<!-- Setup getInitialResourcePriority promises.  -->
<script>
const priority_tests = [
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/dummy.css', location), document),
    expected_priority: kHigh,
    description: 'low importance on <link rel=stylesheet> must load with kHigh priority'
  },
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/dummy.css?1', location), document),
    expected_priority: kVeryHigh,
    description: 'high importance on <link rel=stylesheet> must have no effect on resource load priority'
  },
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/dummy.css?2', location), document),
    expected_priority: kVeryHigh,
    description: 'auto importance on <link rel=stylesheet> must have no effect on resource load priority'
  },
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/dummy.css?3', location), document),
    expected_priority: kVeryHigh,
    description: 'invalid importance on <link rel=stylesheet> must have no effect on resource load priority'
  },
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/dummy.css?4', location), document),
    expected_priority: kVeryHigh,
    description: 'missing importance on <link rel=stylesheet> must have no effect on resource load priority'
  }
];
</script>

<link id=link1 importance=low rel=stylesheet href=../resources/dummy.css>
<link id=link2 importance=high rel=stylesheet href=../resources/dummy.css?1>
<link id=link3 importance=auto rel=stylesheet href=../resources/dummy.css?2>
<link id=link4 importance=xyz rel=stylesheet href=../resources/dummy.css?3>
<link id=link5 rel=stylesheet href=../resources/dummy.css?4>

<script>
  promise_test(async (t) => {
    await new Promise(resolve => {
      addEventListener('DOMContentLoaded', resolve);
    });

    const base_msg = ' was fetched by the preload scanner';
    assert_true(internals.isPreloaded(link1.href), link1.href + base_msg);
    assert_true(internals.isPreloaded(link2.href), link2.href + base_msg);
    assert_true(internals.isPreloaded(link3.href), link3.href + base_msg);
    assert_true(internals.isPreloaded(link4.href), link4.href + base_msg);
    assert_true(internals.isPreloaded(link5.href), link5.href + base_msg);
  }, 'all stylesheets must be fetched by the preload scanner');

  // Setup the tests described by |priority_tests|.
  for (const test of priority_tests) {
    promise_test(async () => {
      const priority = await test.promise;
      assert_equals(priority, test.expected_priority);
    }, test.description);
  }
</script>
